﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using YakShaver.YakServer;
using YakShaver.TFSExtension;
using YakShaver.Contract;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace YakShaver.TFSExtension
{
	public class TFSWorkItemService : IWorkItemProviderService
	{

        private TeamFoundationServer teamFoundationServer = null;
        private ICredentialsProvider credentialsProvider = null;
        private Project tfsProject = null;
        private WorkItemStore workItemStore = null;
        public Project TfsProject
        {
            get { return tfsProject; }
            set { tfsProject = value; }
        }
        
        string serverName = string.Empty;

        public string ServerName
        {
            get { return serverName; }
            set { serverName = value; }
        }

        public ICredentialsProvider CredentialsProvider
        {
            get { return credentialsProvider; }
            set { credentialsProvider = value; }
        }

        #region IWorkItemProviderService Members

        public event EventHandler<WorkItemModifiedEventArgs> WorkItemModifedEvent;

        public void CreateWorkItem(YakShaverWorkItem workItem)
        {
            TFSConnect();
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType workItemType = null;
            string workItemTypeString = string.Empty;

            workItemType = GetWorkItemType(workItem.WorkItemType, tfsProject);

            
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem tfsWorkItem =
                workItemType.NewWorkItem();
            workItemStore.RefreshCache();
            //tfsWorkItem.Store.SyncToCache();
            tfsWorkItem.Store.RefreshCache();
            tfsWorkItem.Title = workItem.Title;
            tfsWorkItem.Description = workItem.Description;
            tfsWorkItem.AreaPath = tfsWorkItem.Store.Projects[tfsProject.Name].AreaRootNodes[workItem.Component].Path;
            tfsWorkItem.IterationPath = tfsProject.IterationRootNodes[workItem.Release].Path;
                   
            // need to add in properties for mapping additinoal data such as assigned to, priority, whatever 
            // else the analysis service decides to do.

            tfsWorkItem.Save();

            workItem.WorkItemId = tfsWorkItem.Id.ToString();
                   
        }

        private static Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType GetWorkItemType(YakShaver.Contract.WorkItemType workItemType, Project project)
        {

            string workItemTypeString = string.Empty;
            switch (workItemType)
            {
                case YakShaver.Contract.WorkItemType.Feature:
                    workItemTypeString = "Scenario";
                    break;
                case YakShaver.Contract.WorkItemType.Issue:
                    workItemTypeString = "Bug";
                    break;
                case YakShaver.Contract.WorkItemType.Task:
                    workItemTypeString = "Task";
                    break;

            }
            return project.WorkItemTypes[ workItemTypeString];
        }

        private void TFSConnect()
        {
            if (CredentialsProvider != null)
                teamFoundationServer = TeamFoundationServerFactory.GetServer(ServerName, CredentialsProvider);
            else
                teamFoundationServer = TeamFoundationServerFactory.GetServer(ServerName);

            teamFoundationServer.Authenticate();


            workItemStore = (WorkItemStore)teamFoundationServer.GetService(typeof(WorkItemStore));
            workItemStore.RefreshCache(); // just make sure we have the latest.
        }

       
        public void ModifyWorkItem(YakShaverWorkItem workItem, IWorkItemDataContract dataContract)
        {
            if (WorkItemModifedEvent != null)
            {
                WorkItemModifedEvent(null, new WorkItemModifiedEventArgs(workItem.InstanceId, dataContract));
            }
        }

        #endregion
    }
}
